//
//  YWMosaicView.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/1.
//

#import "YWMosaicView.h"
#import "MyBezierPath.h"

@interface YWMosaicView ()

//原始view
@property(nonatomic,strong)UIImageView *bgImageView;

//涂鸦的view
@property(nonatomic,strong)UIImageView *drawView;

/** 要绘制的图片 */
@property (nonatomic, strong) UIImage * ogrinalImage;

//要用来涂鸦的图片
@property(nonatomic,strong)UIImage *mosaicImage;

//当前路径的线宽
@property (nonatomic, assign) CGFloat width;


@end

@implementation YWMosaicView

//1、按理来说不应该用一个一个圆圈的叠加的、应该用画笔路径的
//2、美图秀秀的图片马赛克还有个边缘虚化功能，不是单纯的裁剪再重叠图层的方法，所以我现在做的方法是暂时方案实际上不应该这样做
//3、而且现在马赛克做不了用数组来装载操作模型来实现撤回和前进、因为它现在是画好一个圈马上叠加在底层图层了
//4、它现在是直接pan手势中从上下文获取图片、并没有用setNeedDisplay和drawRect方法

-(UIImageView *)bgImageView{
    if(_bgImageView == nil){
        UIImageView *bgImageView = [[UIImageView alloc] init];
        _bgImageView = bgImageView;
    }
    return _bgImageView;
}

-(UIImageView *)drawView{
    if(_drawView == nil){
        UIImageView *drawView = [[UIImageView alloc] init];
        _drawView = drawView;
        drawView.userInteractionEnabled = YES;
        drawView.backgroundColor = [UIColor clearColor];
        
        //添加手势
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [drawView addGestureRecognizer:pan];
    }
    return _drawView;
}



//纯代码用这个
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

//xib用这个
-(void)awakeFromNib{
    [super awakeFromNib];
    [self setup];
}

//初始化
-(void)setup{
    
    [self addSubview:self.bgImageView];
    [self addSubview:self.drawView];
    
    self.width = 10;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.bgImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    self.drawView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}

//设置初始图片颜色
- (void)setFirstImage:(UIImage *)image{
    
    _ogrinalImage = image;
    
    self.bgImageView.image = image;
}

//设置涂鸦图片
-(void)setNeedMosaicImage:(UIImage *)image{
    
    _mosaicImage = image;
    
}


//设置线的宽度
- (void)setLineWith:(CGFloat)lineWidth{
    self.width = lineWidth;
}

//获取当前图层
-(UIImage *)getCurrentLayer{
    
    //1.开启一个位图上下文
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    
    //2.把画板上的内容渲染到上下文当中
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.layer renderInContext:ctx];
    
    //3.从上下文当中取出一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //4.关闭上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}


- (void)pan:(UIPanGestureRecognizer *)pan {
    
    //获取的当前手指的点
    CGPoint curP = [pan locationInView:self.drawView];
    
    //获取上下文
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    
    
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    [self.drawView.layer renderInContext:ctx];
    
    CGFloat rectWH = self.width;
    CGFloat x = curP.x - rectWH * 0.5;
    CGFloat y = curP.y - rectWH * 0.5;
    
    //4.绘制一个小圆,把小圆设置成裁剪区域
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, rectWH, rectWH)];
    [clipPath addClip];
    
    //5.把图片绘制到上下文当中
    if(self.mosaicImage != nil){
        [self.mosaicImage drawInRect:self.bounds];
    }
    else{
        [[UIImage imageNamed:@"b2"] drawInRect:self.bounds];
    }
    
    //从上下文当中生成 一张图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    //重新赋值
    self.drawView.image = newImage;
    
    //关闭上下文
    UIGraphicsEndImageContext();
    
    
}


@end
